#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int n;
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	int ans = 0;
	for (int i = 0; i < n - 1; i++) {
		if (arr[i + 1] > arr[i]) {
			ans += arr[i + 1] - arr[i];
		}
	}
	cout << ans << endl;
	return 0;
}